package com.jcnet.bizaia.view;

import com.baidu.cyberplayer.core.BMediaController;
import com.baidu.cyberplayer.core.BVideoView;
import com.baidu.cyberplayer.utils.bv;
import com.jcnet.bizaia.App;
import com.jcnet.bizaia.R;
import com.jcnet.bizaia.VideoViewPlayingActivity;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class JCSmailVideoView extends LinearLayout implements OnClickListener{

	private Context context;
	
	private BVideoView bVideoView;
	
	private String videoPath;
	
	private Button playButton;
	
	private LinearLayout mainLinearLayout;
	
	private boolean isFirstIn = true;
	
	public JCSmailVideoView(Context context,String videoPath) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.videoPath = videoPath;
		initView();
	}

	public JCSmailVideoView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context = context;
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		
		View view = View.inflate(context, R.layout.head_video_lay, this);
		
		
		mainLinearLayout = (LinearLayout)view.findViewById(R.id.viewMianLin);
		
		playButton = (Button) view.findViewById(R.id.playButton);
		
		playButton.setOnClickListener(this);
		
		bVideoView  = (BVideoView) view.findViewById(R.id.video_view);

		bVideoView.setAKSK(App.BAIDU_AK, App.BAIDU_SK);

		//bVideoView.setMediaController(mVVCtl); 
		bVideoView.setDecodeMode(BVideoView.DECODE_SW); //可选择软解模式或硬解模式 
		//Uri.parse("http://devimages.apple.com/iphone/samples/bipbop/gear4/prog_index.m3u8")
		bVideoView.setVideoPath(videoPath);
		bVideoView.showCacheInfo(true);
		bVideoView.setVideoScalingMode(BVideoView.VIDEO_SCALING_MODE_SCALE_TO_FIT);
		
	
		
		mainLinearLayout.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if(bVideoView.isPlaying()){
					bVideoView.setBackgroundResource(R.drawable.play_bg);
				}else{
					bVideoView.setBackgroundResource(R.drawable.pause_bg);
				}
				return false;
			}
		});
		
	}

	
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.playButton:
			
			
			if(bVideoView.isPlaying()){
				bVideoView.pause();
				playButton.setBackgroundResource(R.drawable.pause_bg);
			}else{
				if(isFirstIn){
					bVideoView.start();
					isFirstIn = false;
				}else{
				bVideoView.resume();
				}
				playButton.setBackgroundResource(R.drawable.play_bg);
			}
			
			break;

		default:
			break;
		}
	}
	
	

}
